package ipt.android.application;


import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;


public class Conectar extends AsyncTask<String, Integer, List<Object>> {
	

	private ProgressDialog mProgressDialog;
	private Context context;
	private final String SERVIDOR = "http://10.9.56.75:8080/dpesp-indicacao/usuario/authenticate";
	private final String LISTAR = "http://10.9.56.75:8080/dpesp-indicacao/aviso/list";
	private final String FIAP = "http://www.fiap.com.br";
	
	public Conectar(Context context){
		this.context = context;
	}
	
	
	protected List<Object> doInBackground(String... params) {
		
		
		String responseListaString = null;
		// Creating HTTP client
        HttpClient httpClient = new DefaultHttpClient();
        // Creating HTTP Post
        HttpPost httpPost = new HttpPost(this.SERVIDOR);
        HttpGet httpGet = new HttpGet(LISTAR);
 
        // Building post parameters
        // key and value pair
        List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(2);
        nameValuePair.add(new BasicNameValuePair("usuario.login", "sramos"));
        nameValuePair.add(new BasicNameValuePair("usuario.senha", "12345678"));
 
        // Url Encoding the POST parameters
        try {
            httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair));
        } catch (UnsupportedEncodingException e) {
             //writing error to Log
            e.printStackTrace();
        }
 
        // Making HTTP Request
        try {
            HttpResponse response = httpClient.execute(httpPost);
            String responseString = EntityUtils.toString(response.getEntity());
            // writing response to log
            Log.d("Http Response:", response.toString());
            Log.d("Http Response Content:", responseString);
            HttpResponse responseLista = httpClient.execute(httpGet);
            responseListaString = EntityUtils.toString(responseLista.getEntity());
            /*JSONObject jsonObject = new JSONObject(responseListaString);
            JSONArray jsonArray = jsonObject.getJSONArray("avisos");
            
            jsonArray.getJSONObject(0).get("id");
            jsonArray.getJSONObject(0).getJSONObject("dataInclusao").get("$").toString();
            
            //Converter data do JSON
            Date data = (Timestamp) jsonArray.getJSONObject(0).getJSONObject("dataInclusao").get("$");
            
            SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");*/
            
            Log.d("Http Response:", responseLista.toString());
            Log.d("Http Response Content:", responseListaString);
        } catch (ClientProtocolException e) {
            // writing exception to log
            e.printStackTrace();
        } catch (IOException e) {
            // writing exception to log
            e.printStackTrace();
 
        } /*catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}*/
        List<Object> listaObjects = new ArrayList<Object>();
        listaObjects.add(responseListaString);
		return listaObjects;
	}

	@Override
	protected void onPostExecute(List<Object> result) {
		// TODO Auto-generated method stub
		mProgressDialog.dismiss();
		super.onPostExecute(result);
	}
	
    @Override
    protected void onPreExecute() {
           mProgressDialog = ProgressDialog.show(context, "", "Loading...",true,false);
        super.onPreExecute();
    }

}
